import React from 'react'
import {Modal, Form, Input, message} from 'antd'
import axios from 'axios'
import PropTypes from 'prop-types'
import api from '../../../../api/api'
const FormItem = Form.Item;
@Form.create()
class Addcompany extends React.Component {


  constructor(props) {
    super(props)
    this.state = { 
      param: {
        account: "",
        appId: "",
        companyId: "",
        companyName: "",
        createTime: "",
        parentCompanyId: "",
        password: "",
        thirdCompanyId: "",
        orgCode:""
      }
    };
  }
  componentDidMount() {
    this.clearData()
    this.props.onRef(this)
  }
  handleOk = () => {
    this.props.form.validateFields((error, values) => {
      if(error != null){
        return;
      }
      let { param } = this.state
      param.companyName = values.companyName
      param.account = values.account
      param.password = values.password
      param.orgCode = localStorage.getItem('orgCode')
      this.setState({ param }, () => {
        this.companyData()
      })
    })
  }
  //编辑的初始值设置
  setBaseInfo = () => {
    let data = this.props.data.gridList
    const { form } = this.props;
    Object.keys(form.getFieldsValue()).forEach(key => {
      const obj = {};
      if (data[key]) {
        obj[key] = data[key] || null;
      }
      form.setFieldsValue(obj);
    });
  };
  //清空form表单数据
  clearData() {
    this.props.form.resetFields();
  }
  //初始化编辑数据
  initEditData = () => {
    this.setBaseInfo()
  }
  companyData() {
    let url = ""
    if(this.props.data.type === 2) {
      url = api.api_URL + "/admin/company/updateCompanyInfo"
      let { param } = this.state
      param.companyId = this.props.data.gridList.companyId
      this.setState({ param }, () => {
        this.addCompany(url)
      })
    }else{
      url = api.api_URL + "/admin/company/addCompanyInfo"
      let { param } = this.state
      if(this.props.data.type === 1) {
        param.parentCompanyId = ""
        this.setState({ param }, () => {
          this.addCompany(url)
        })
      }else{
        param.parentCompanyId = this.props.data.gridList.companyId
        this.setState({ param }, () => {
          this.addCompany(url)
        })
      }

    }
  }
  //添加
  addCompany(url) {
    const that = this;
    axios({
      method: 'POST',
      url: url,
      data: JSON.stringify(that.state.param),
      headers: {
        "Authorization": localStorage.getItem('token'),
        "Content-Type": "application/json;charset=UTF-8"
      },
    }).then(function (response) {
      message.success({content:"成功！"})
      that.handleCancel()
    })
      .catch(function (error) {
        console.log(error);
        message.error({content:"失败,请重新尝试 ！"})

      })
  }
  handleCancel = e => {
    console.log(e);
    this.props.handleCancel()
  };
  render() {
    //getFieldError  验证表单需要加 
    const { getFieldDecorator, getFieldError } = this.props.form
    // const { getFieldDecorator } = this.props.form
    return (
      <div>
        <Modal
          title={this.props.data.title}
          visible={this.props.data.visible}
          onOk={this.handleOk}
          onCancel={this.handleCancel}
        >
          <Form name="control-ref" autoComplete="off" ref={this.formRef}
            labelCol={{ span: 4 }}
            wrapperCol={{ span: 18 }}
          >
            <FormItem label="公司名称" name="companyName">
              {getFieldDecorator("companyName", {})(
                <Input />
              )}
            </FormItem>
            <FormItem label="账号" name="account">
              {getFieldDecorator("account", {})(
                <Input />
              )}
            </FormItem>
            <FormItem label="密码" name="password">
              {getFieldDecorator("password", {})(
                <Input.Password />
              )}
            </FormItem>
          </Form>
        </Modal>
      </div>
    );
  }
}
export default Addcompany